package cn.tedu.smartos.family_user.controller;

import cn.tedu.smartos.base.response.JsonResult;
import cn.tedu.smartos.family_user.pojo.dto.ElderlyBindParam;
import cn.tedu.smartos.family_user.pojo.vo.BoundElderlyVO;
import cn.tedu.smartos.family_user.pojo.vo.ElderlyHealthInfoVO;
import cn.tedu.smartos.family_user.service.ElderlyBindService;
import cn.tedu.smartos.family_user.service.ElderlyHealthService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

@Api(tags = "老人绑定模块")
@Slf4j
@RestController
@RequestMapping("/api/v1/family_user/elderly")
public class ElderlyBindController {
    @Autowired
    private ElderlyBindService elderlyBindService;
    @Autowired
    private ElderlyHealthService elderlyHealthService;
    @ApiOperation("添加并绑定老人")
    @PostMapping("/bind")
    public JsonResult bindElderly(@RequestBody ElderlyBindParam bindParam) {
        log.info("开始处理【绑定老人】请求, 家属ID: {}", bindParam.getFamilyUserId());
        Long elderlyId = elderlyBindService.bindElderly(bindParam);
        return JsonResult.ok(elderlyId);
    }

    @ApiOperation("查看老人健康信息")
    @GetMapping("/{elderlyUserId}")
    public JsonResult getHealthInfo(@PathVariable Long elderlyUserId) {
        log.info("查看老人健康信息: elderlyUserId={}", elderlyUserId);
        ElderlyHealthInfoVO healthInfo = elderlyHealthService.getHealthInfo(elderlyUserId);
        return JsonResult.ok(healthInfo);
    }

    @ApiOperation("获取家属绑定的老人列表")
    @GetMapping("/boundElderly/{familyUserId}")
    public JsonResult getBoundElderly(@PathVariable Long familyUserId) {
        log.info("获取家属绑定的老人列表: familyUserId={}", familyUserId);
        List<BoundElderlyVO> boundElderlyList = elderlyBindService.getBoundElderly(familyUserId);
        return JsonResult.ok(boundElderlyList);
    }
    @ApiOperation("家属取消绑定老人")
//    @DeleteMapping("/unbind")
    @PostMapping("/unbind")
    public JsonResult unbindElderly(@RequestBody UnbindParam param) {
        log.info("家属取消绑定老人: familyUserId={}, elderlyUserId={}",
                param.getFamilyUserId(), param.getElderlyUserId());
        elderlyBindService.unbind(param.getFamilyUserId(), param.getElderlyUserId());
        return JsonResult.ok();
    }

    @Data
    public static class UnbindParam {
        @NotNull
        private Long familyUserId;
        @NotNull
        private Long elderlyUserId;
    }

    // 确保两个接口返回相同数据结构
    @ApiOperation("按条件查询家属已绑定的老人列表")
    @GetMapping("/boundElderly/search")
    public JsonResult searchBoundElderly(@RequestParam Long familyUserId,
                                         @RequestParam(required = false) String relationType,
                                         @RequestParam(required = false) String realName) {
        // 使用相同的服务方法获取数据
        List<BoundElderlyVO> result = elderlyBindService.getBoundElderly(
                familyUserId, relationType, realName
        );
        return JsonResult.ok(result);
    }
}
